package com.cz.algorithm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

public class LogShardingDbAlgorithm implements StandardShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> db, PreciseShardingValue<Date> shardingValue) {
        Date date = shardingValue.getValue();
        String dateStr = DateUtil.format(date, "yyyyMM");
        List<String> list1 =new ArrayList<>();
        list1.add("202401");
        list1.add("202402");
        list1.add("202403");
        list1.add("202404");
        list1.add("202405");
        list1.add("202406");

        List<String> list2 =new ArrayList<>();
        list2.add("202407");
        list2.add("202408");
        list2.add("202409");
        list2.add("202410");
        list2.add("202411");
        list2.add("202412");
        if(CollUtil.contains(list1,dateStr)){
            return "ds_1";
        } else if (CollUtil.contains(list2,dateStr)) {
            return "ds_2";
        }
        return "";
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Date> shardingValue) {
        return null;
    }
}
